文档

使用二级索引读取数据

表格存储提供了单行读取和范围读取的查询方式用于读取索引表中数据。当返回的属性列在索引表中时,您可以直接读取索引表获取数据,否则请自行反查数据表获取数据。

前提条件

注意事项

  • 索引表只能用于读取数据。

  • 当需要返回的属性列不在索引表中时,您需要自行反查数据表来获取数据。

单行读取数据

调用GetRow接口读取一行数据。更多信息,请参见读取单行数据

参数

使用GetRow接口读取索引表中数据时有如下注意事项:

  • tableName需要设置为索引表名称。

  • 由于系统会自动将未出现在索引列中的数据表主键补齐到索引表主键中,所以设置行的主键时,需要同时设置索引表索引列和补齐的数据表主键。

示例

以下示例用于读取索引表中指定主键的行数据。

public static void GetRowfromIndex()
{
    OTSClient otsClient = Config.GetClient();
    //构造主键。构造主键时必须带上补齐到索引表中的数据表主键。
    PrimaryKey primaryKey = new PrimaryKey
    {
        { "col0", new ColumnValue(0) },
        { "pk1", new ColumnValue(0) },
        { "pk2", new ColumnValue("abc") }
    };

    try
    {
        //构造查询请求对象,此处未指定读取的列,默认读取整行数据。
        var request = new GetRowRequest(IndexName, primaryKey);

        //调用GetRow接口查询数据。
        var response = otsClient.GetRow(request);

        Console.WriteLine("Primary Key: " + response.PrimaryKey);
        Console.WriteLine("Attribute Column: " + response.Columns[0]);

        //如果没有抛出异常,则说明执行成功。
        Console.WriteLine("Get row succeeded.");
    }
    catch (Exception ex)
    {
        //如果抛出异常,则说明执行失败,处理异常。
        Console.WriteLine("Get row failed, exception:{0}", ex.Message);
    }
}

范围读取数据

调用GetRange接口读取一个范围内的数据。更多信息,请参见范围读取数据

参数

使用GetRange接口读取索引表中数据时有如下注意事项:

  • tableName需要设置为索引表名称。

  • 由于系统会自动将未出现在索引列中的数据表主键补齐到索引表主键中,所以设置起始主键和结束主键时,需要同时设置索引表索引列和补齐的数据表主键。

示例

以下示例用于读取索引表中指定主键范围内的数据。其中第一列主键col1的列值为0。

public static void GetRangeFromIndexTable()
{
    Console.WriteLine("Start getRange from index...");
    OTSClient otsClient = Config.GetClient();
    //指定第一主键Col1的值,进行扫描。
    PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey
    {
        { "col1", new ColumnValue(0) },
        { "pk1",  ColumnValue.INF_MIN },
        { "pk2", ColumnValue.INF_MIN }
    };

    PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey
    {
        { "col1", new ColumnValue(0) },
        { "pk1",  ColumnValue.INF_MAX },
        { "pk2", ColumnValue.INF_MAX }
    };

    GetRangeRequest request = new GetRangeRequest(IndexName, GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey);

    GetRangeResponse response = otsClient.GetRange(request);
    IList<Row> rows = response.RowDataList;
    PrimaryKey nextStartPrimaryKey = response.NextPrimaryKey;
    while (nextStartPrimaryKey != null)
    {
        request = new GetRangeRequest(TableName, GetRangeDirection.Forward, nextStartPrimaryKey, exclusiveEndPrimaryKey);
        response = otsClient.GetRange(request);
        nextStartPrimaryKey = response.NextPrimaryKey;
        foreach (var row in response.RowDataList)
        {
            rows.Add(row);
        }
    }

    foreach (var row in rows)
    {
        PrintRow(row);
    }

    Console.WriteLine("TotalRowsRead: " + rows.Count);
}

private static void PrintRow(Row row)
{
    Console.WriteLine("-----------------");

    foreach (KeyValuePair<string, ColumnValue> entry in row.GetPrimaryKey())
    {
        Console.WriteLine(entry.Key + ":" + entry.Value);
    }

    foreach (Column entry in row.GetColumns())
    {
        Console.WriteLine(entry.Name + ":" + entry.Value);
    }

    Console.WriteLine("-----------------");
}             

常见问题

相关文档

  • 当日常业务中有非主键列查询、多列组合查询、模糊查询等多维查询需求以及求最值、统计行数、数据分组等数据分析需求时,您可以将这些属性作为多元索引中的字段并使用多元索引查询与分析数据。 更多信息,请参见多元索引

  • 如果需要使用SQL查询和分析数据,您可以使用SQL查询功能实现。更多信息,请参见SQL查询

  • 本页导读 (1)
文档反馈